// 媒体控制事件总线
import 'package:flutter/foundation.dart';

/// 媒体控制事件类型
enum MediaControlEvent {
  playPause,
  stop,
  rewind,
  fastForward,
  volumeUp,
  volumeDown,
  mute,
  nextTrack,
  previousTrack,
}

/// 媒体控制事件监听器回调
typedef MediaControlListener = void Function(MediaControlEvent event);

/// 媒体控制事件总线单例
class MediaControlEventBus {
  static final MediaControlEventBus instance = MediaControlEventBus._internal();
  
  static final MediaControlEventBus _instance = MediaControlEventBus._internal();
  
  factory MediaControlEventBus() => _instance;
  
  MediaControlEventBus._internal();
  
  /// 事件监听器列表
  final List<MediaControlListener> _listeners = [];
  
  /// 添加事件监听器
  void addListener(MediaControlListener listener) {
    if (!_listeners.contains(listener)) {
      _listeners.add(listener);
    }
  }
  
  /// 移除事件监听器
  void removeListener(MediaControlListener listener) {
    _listeners.remove(listener);
  }
  
  /// 广播媒体控制事件
  void broadcast(MediaControlEvent event) {
    if (kDebugMode) {
      print('广播媒体控制事件: $event');
    }
    
    // 通知所有监听器
    for (final listener in List<MediaControlListener>.from(_listeners)) {
      try {
        listener(event);
      } catch (e) {
        if (kDebugMode) {
          print('媒体控制事件监听器执行错误: $e');
        }
      }
    }
  }
  
  /// 清空所有监听器
  void clearAllListeners() {
    _listeners.clear();
  }
  
  /// 获取监听器数量
  int get listenerCount => _listeners.length;
}